import 'dart:async';

import 'package:flutter/material.dart'; 
import 'package:fluttertoast/fluttertoast.dart';
import './MyButton.dart';
// 直接引插件  pub.dev   -> flutter_swiper
class AlertPage extends StatefulWidget {
  AlertPage({Key key}) : super(key: key);

  _AlertPageState createState() => _AlertPageState();
}

class _AlertPageState extends State<AlertPage> {
  _alertDialog() async{
    var result = await showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          title: Text("提示信息"),
          content: Text("删除？"),
          actions: <Widget>[
            FlatButton(
              child: Text("取消"),
              onPressed: (){
                Navigator.pop(context,'cancle'); //关闭提示创
              },
            ),
            FlatButton(
              child: Text("确定"),
              onPressed: (){
                Navigator.pop(context,'ok');
              },
            )
          ],
        );
      }
    );
    print(result);
  }
  _simpleDialog() async{
    var result = await showDialog(
      context: context,
      builder: (context){
        return SimpleDialog(
          title: Text("选择内容"),
          children: <Widget>[
            SimpleDialogOption(
              child: Text("A"),
              onPressed: (){
                Navigator.pop(context,'A'); //关闭提示创
              },
            ),
            SimpleDialogOption(
              child: Text("B"),
              onPressed: (){
                Navigator.pop(context,'B'); //关闭提示创
              },
            )
        ],);
      }
    );
    print(result);
  }
  _showModalBottomSheet() async{
    var result = await showModalBottomSheet(
      context: context,
      builder: (context){
        return Container(
          height: 200,
          child: Column(
            children: <Widget>[
              ListTile(
                title: Text("分享A"),
                onTap: (){
                  Navigator.pop(context,'A'); //关闭提示创
                },
              ),
              ListTile(
                title: Text("分享B"),
                onTap: (){
                  Navigator.pop(context,'B'); //关闭提示创
                },
              ),
              ListTile(
                title: Text("分享C"),
                onTap: (){
                  Navigator.pop(context,'C'); //关闭提示创
                },
              )
            ],
          ),
        );
      }
    );
    print(result);
  }
  _toast(){
    Fluttertoast.showToast(
      msg: "提示信息",
    );
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
         appBar: AppBar(
           title: Text("AlertPage"),
         ),
         body: Center(
           child: Column(
           children: <Widget>[
             MyButton(text: '弹出框 - alertDialog',pressed:(){
               _alertDialog();
             }),
             Divider(),
             MyButton(text: '弹出选择框 - SimpleDialog',pressed:(){
               _simpleDialog();
             }),
             Divider(),
             MyButton(text: '底部弹出框 - ShowModalBottomSheet',pressed:(){
               _showModalBottomSheet();
             }),
             Divider(),
             MyButton(text: 'toast - fluttertoast第三方',pressed:(){
                _toast();
             }),
             Divider(),
             MyButton(text: '自定义 Dialog',pressed:(){
                showDialog(
                  context: context,
                  builder: (context){
                    return MyDialog(
                      title: "关于我们",
                      content: "我是内容我是内容我是内容我是内容我是内容我是内容",
                    );
                  }
                );
             }),
           ],
         )
       ),
      );
  }
}

class MyDialog extends Dialog {
  String title;
  String content;
  MyDialog({this.content = '',this.title = ''});
  // 定时器关闭
  _showTimer(context){
    var timer;
    timer = Timer.periodic(
      Duration(milliseconds: 3000),(t){
        Navigator.pop(context);
        t.cancel();  //取消定时器
      });
  }
  @override
  Widget build(BuildContext context) {
    _showTimer(context);
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          width: 250,
          height: 250,
          color: Colors.white,
          child: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.all(10),
                child: Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.center,
                        child: Text(this.title),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: InkWell(
                          child: Icon(Icons.close),
                          onTap: (){
                            
                            
                          },
                        ),
                      ),
                    ],
                  )
              ),
              Divider(),
              Container(
                padding: EdgeInsets.only(left: 10.0,right: 10),
                width: double.infinity, 
                child: Text(this.content,textAlign: TextAlign.left,),
              )
            ],
          ),
        ),
      ),
    );
  }
}